from __future__ import absolute_import
import os
import os.path
import glob
import logging
import shutil
#from os.path import isdir, join
from forest.registry import register_tool
from forest.tool import Tool


logger = logging.getLogger('tools.system.remove')

@register_tool('system.remove')
def remove(_target):
    logger.log(logging.TRACE, 'Remove file: %s', _target)

    for target in glob.glob(_target):
        # check broken and valid links
        # same truth value as this or <same-or-false>

        if not os.path.lexists(target):
            return

        if os.path.isdir(target) and not os.path.islink(target):
            shutil.rmtree(target)
        else:
            os.unlink(target)
